package com.cuitrip.business.order.detail.ui.viewholder;

import android.view.View;
import android.view.ViewStub;

import com.cuitrip.app.base.PartViewHolder;

/**
 * Created by ak on 15/12/17.
 */
public class StubViewHolder<T> implements PartViewHolder<T> {
    PartViewHolder<T> partViewHolder;
    ViewStub stub;
    View inflatedView;
    boolean isInflated = false;

    public StubViewHolder(PartViewHolder<T> partViewHolder) {
        this.partViewHolder = partViewHolder;
    }

    public PartViewHolder<T> getPartViewHolder() {
        return partViewHolder;
    }

    @Override
    public void build(View view) {
        stub = (ViewStub) view;

    }

    @Override
    public void render(T o) {
        if (o != null) {
            if (!isInflated) {
                isInflated = true;
                inflatedView = stub.inflate();
                partViewHolder.build(inflatedView);
                partViewHolder.render(o);
            } else {
                partViewHolder.render(o);
            }
            show();
        } else {
            hide();
        }
    }

    void show() {
        if (isInflated) {
            inflatedView.setVisibility(View.VISIBLE);
        } else {
            stub.setVisibility(View.VISIBLE);
        }
    }

    void hide() {
        if (isInflated) {
            inflatedView.setVisibility(View.GONE);
        } else {
            stub.setVisibility(View.GONE);
        }
    }
}